﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FeedAntGame.Sprite.Event
{
    class EventSprite : EventBaseSprite
    {

        protected Texture2D normalBackground;
        protected Texture2D clickBackground;
        protected Texture2D hoverBackground;
        protected Texture2D disableBackground;
                
        protected bool toogle = false;
        protected bool _focus = false;
        protected bool hoverOut = false;
        private bool isReadyToClick = false;
        
        //Event 
        public delegate void ClickButton();
        public event ClickButton Click;
        public delegate void HoverButton();
        public event HoverButton Hover;

        /************************************************************************/
        /*                      Get/Set                                                                     */
        /************************************************************************/
        /// <summary>
        /// Always false if State is NORMAL. It's true if we set it. Not automatic !
        /// </summary>
        public bool HoverOut
        {
            get { return hoverOut; }
            set { hoverOut = value; }
        }
        /// <summary>     
        /// False when click out of this. True if click in this
        /// </summary>        
        public bool Focus
        {
            get
            {
                return _focus;
            }
            set
            {
                _focus = value;
            }
        }
        /// <summary>
        /// Change when you Click on sprite
        /// </summary>
        public virtual bool Toogle
        {
            get { return toogle; }
            set { toogle = value; }
        }

        public override STATE_TYPE State 
        {
            get
            {
                return _state;
            }
            set
            {
                _state = value;
                switch (_state)
                {
                    case STATE_TYPE.NORMAL:
                        this.textureImage = normalBackground;
                        break;
                    case STATE_TYPE.CLICK:
                        this.textureImage = clickBackground;
                        break;
                    case STATE_TYPE.HOVER:
                        this.textureImage = hoverBackground;
                        break;
                    case STATE_TYPE.DISABLE:
                        this.textureImage = disableBackground;
                        break;
                }
            }
        }
        
        /************************************************************************/
        /*                      Constructor                                                                     */
        /************************************************************************/
        public EventSprite( Texture2D clickBackground, Texture2D normalBackground, Texture2D hoverBackground,
            Texture2D disableBackground, Vector2 position, Point frameSize,
            int collisionOffset, Point sheetSize, Vector2 speed, string collisionCueName, float scale)
            : base( position, frameSize, collisionOffset, sheetSize, speed, collisionCueName, scale)
        
        {
            this.normalBackground = normalBackground;
            this.hoverBackground = hoverBackground;
            this.clickBackground = clickBackground;
            this.disableBackground = disableBackground;

            this.textureImage = normalBackground;
        }


        /************************************************************************/
        /*                          Functions                                                                     */
        /************************************************************************/
        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            MouseState mouseState = Mouse.GetState();
            Rectangle mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1);
            if (Enable)
            {
                if ((Origin == Vector2.Zero && this.collisionRect.Intersects(mouseRect))
                    || (Origin == new Vector2(textureImage.Width / 2, textureImage.Height / 2) &&
                    mouseState.X >= Position.X - Width / 2 && mouseState.X <= Position.X + Width - Width / 2
                && mouseState.Y >= Position.Y - Height / 2 && mouseState.Y <= Position.Y + Height - Height / 2))
                {
                    if (mouseState.LeftButton == ButtonState.Released)
                    {
                        isReadyToClick = true;
                    }

                    if (mouseState.LeftButton == ButtonState.Pressed)
                    {
                        if (State == STATE_TYPE.HOVER)
                        {
                            State = STATE_TYPE.CLICK;

                            if (toogle)
                            {
                                Toogle = false;
                            }
                            else
                            {
                                Toogle = true;

                            }
                        }
                        else if (State == STATE_TYPE.CLICK)
                        {
                            State = STATE_TYPE.NORMAL;
                        }
                        if (Click != null && isReadyToClick)
                        {
                            onClick();
                        }
                        _focus = true;
                    }
                    else
                    {

                        if (State != STATE_TYPE.HOVER)
                        {
                            State = STATE_TYPE.HOVER;
                        }

                        if (Hover != null)
                        {
                            this.Hover();
                        }
                    }
                }
                else
                {
                    isReadyToClick = false;
                    State = STATE_TYPE.NORMAL;
                    {
                        if (mouseState.LeftButton == ButtonState.Pressed)
                        {
                            _focus = false;                            
                        }
                        hoverOut = false;
                    }
                }
            }
            
            base.Update(gameTime, clientBounds);
        }

        protected virtual void onClick()
        {
            this.Click();
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }

        public bool IsPlaySoundHover()
        {
            if (State == EventSprite.STATE_TYPE.HOVER)
            {
                if (collisionCueName != null && collisionCueName.Length != 0)
                {
                    if (HoverOut != true)
                    {
                        HoverOut = true;
                        return true;
                    }

                }
            }
            return false;
        }
    }
}
